﻿Public Class FrmSQLQuery
    Dim mysql1 As New ClsSQLhelper
    Dim Strsql As String
    Dim On_load As Boolean = True
    Function Load_Data(ByVal Strsql As String) As Boolean
        Try
            Dim DS As New DataSet
            Dim Mysql1 As New ClsSQLhelper
            Load_Data = False
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = Mysql1.GetMYSQLDataset(Strsql, "DS_CustomQuery", "CustomQuery")
            Pn_Framework.Set_GridSetting(Me.griddetail, DS)
            Load_Data = True

        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)
        End Try

    End Function
    Private Sub FrmSQLQuery_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_ComboboxData_Tables()
        If My.Settings.SQLTools_Selected_Table <> "" Then
            Me.CboTable.Text = My.Settings.SQLTools_Selected_Table
        End If
        If My.Settings.SQLTools_Current_Query <> "" Then
            Me.TxtSQLString.Text = My.Settings.SQLTools_Current_Query
        End If
        On_load = False
    End Sub

    Private Sub BtnExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExecute.Click
        If MsgBox("Do you want to Excute this SQL Command?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Excute SQL Command") = MsgBoxResult.No Then Exit Sub
        mysql1.MySQLExecute(Strsql)
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub BtnRunQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRunQuery.Click
        If MsgBox("Do you want to run this query?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Run Query") = MsgBoxResult.No Then Exit Sub
        Load_Data(Me.TxtSQLString.Text)
        My.Settings.SQLTools_Current_Query = Me.TxtSQLString.Text
    End Sub
    Function Load_ComboboxData_Tables() As Boolean
        Try
            Dim DS As New DataSet

            Load_ComboboxData_Tables = False

            Strsql = "show tables"
            Me.EventLog1.WriteEntry(Me.Name & " : " & Strsql, EventLogEntryType.Information, 1)
            DS = mysql1.GetMYSQLDataset(Strsql, "DS_tables_in_whis", "tables_in_whis")
            ' Me.EventLog1.WriteEntry(Strsql, EventLogEntryType.Information)
            With Me.CboTable
                If DS.Tables(0).Rows.Count = 0 Then Exit Function
                .DataSource = DS.Tables(0)
                .DisplayMember = "tables_in_whis"
                .ValueMember = "tables_in_whis"
                .SelectionStart = 0
            End With
            Load_ComboboxData_Tables = True
        Catch ex As Exception
            Me.EventLog1.WriteEntry(Me.Name & " : Function Load_ComboboxData_tables - " & ex.Message, EventLogEntryType.Error, 1)
            MsgBox(ex.Message)

        End Try
    End Function

    Private Sub CboTable_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboTable.SelectedIndexChanged
        If On_load = True Then Exit Sub
        Me.TxtSQLString.Text = "Select * from " & Me.CboTable.SelectedValue
        My.Settings.SQLTools_Selected_Table = Me.CboTable.Text
    End Sub
End Class